/*
* Copyright (c) 2009 contentismissing.net, artifacts.de
*
* All rights reserved.
*
* Authors: Jens Lembke <j@contentismissing.net>;
*          Michael Markowski <m.markowski@artifacts.de>;
*
* All non-exclusive rights granted to Deutsche Telekom AG Laboratories
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
package net.contentismissing.tlabs.cocktailparty.ui
{

	import de.artifacts.multitouch.*;
	import de.artifacts.multitouch.behaviours.*;
	
	import flash.display.*;
	import flash.events.Event;
	import flash.geom.Point;
	
	import mx.core.Application;
	
	import net.contentismissing.tlabs.cocktailparty.behaviours.BubbleDragBehaviour;
	
	import org.cove.ape.*;
	
	public class Workspace extends MovieClip
	{
		
		public var _physicsDefaultGroup : Group;
		public var _physicsMenuGroup : Group;
		public var bg : MovieClip;
		public var touchManager : TouchManager;
		public var bubbleContainer : Sprite;
		
		
		private var emitter : ITouchEventProvider;
		private var touchPoint : Point;
		private var touchArea : Sprite;
		
		
		
		public function Workspace()
		{
			super();
			
			touchArea = new Sprite();
			emitter = new TouchEventSimulatorProvider(this); // aktiviert Mouse Simulator 
			touchManager = new TouchManager(touchArea, emitter);
				
			bg = new background_mc();
			this.addChild(bg);
			
			bubbleContainer = new Sprite;
			this.addChild(bubbleContainer);
			
			/* APE Engine start */
			APEngine.init(1 / 2);
			APEngine.container = this;
			//APEngine.constraintCollisionCycles = 228;
			//APEngine.constraintCycles = 229;
			//APEngine.damping = 0;
			//APEngine.addForce(new ApeVector(0.0,0.0));
			APEngine.addGroup(this.physicsDefaultGroup);
			APEngine.addGroup(this.physicsMenuGroup);
			
			addEventListener(Event.ENTER_FRAME , run);
			
			/* Setup Start Bubbles Constants.startupBubbels */					
			for (var i : int = 1; i <= 4; i++)
			{
				var offsetScope : int = 360;
				var randomPoint : Point = new Point(randRange( - offsetScope , offsetScope) , randRange( - offsetScope , offsetScope));
				
				var startPoint : Point = new Point;
				startPoint.x = (Application.application.width / 2) + randomPoint.x;
				startPoint.y = (Application.application.height / 2) + randomPoint.y;
				
				this.addBubble(i, startPoint.x , startPoint.y);
				
			}
			
		}
		
		private function addBubble(id:Number, x : int , y : int , scaleX : Number = 1 , scaleY : Number = 1) : void
		{
			
			var bubble : Bubble = new Bubble(id , this );
			
			touchManager.makeTouchable(bubble , 1);
			touchManager.addBehaviour(bubble , new BubbleDragBehaviour());
			touchManager.addBehaviour(bubble , new ScaleBehaviour());
			touchManager.addBehaviour(bubble , new RotateBehaviour());
			
			bubble.x = x;
			bubble.y = y;
			bubble.scaleX = scaleX;
			bubble.scaleY = scaleY;
			
			this.physicsDefaultGroup.addParticle(bubble.particle);
			
			this.addChild(bubble.particle.sprite);
			
		}

		public function run(evt : Event) : void
		{
			APEngine.step();
			APEngine.paint();
		}

		public function randRange(start : Number , end : Number) : Number
		{
			return Math.floor(start + (Math.random() * (end - start)));
		}

		public function get physicsMenuGroup() : Group
		{
			if (_physicsMenuGroup == null)
			{
				_physicsMenuGroup = new Group();
				_physicsMenuGroup.collideInternal = true;
			}

			return _physicsMenuGroup;
		}

		public function set physicsMenuGroup(group : Group) : void
		{
			this._physicsMenuGroup = group;
		}

		public function get physicsDefaultGroup() : Group
		{
			if (_physicsDefaultGroup == null)
			{
				_physicsDefaultGroup = new Group();
				_physicsDefaultGroup.collideInternal = true;
			}

			return _physicsDefaultGroup;
		}

		public function set physicsDefaultGroup(group : Group) : void
		{
			this._physicsDefaultGroup = group;
		}
		
	}
}
